﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class msg
    {
        public static dynamic getDiscussTree(int? pid, int? did)
        {
            var sql = new Sql.SqlDataContext();
            if (did != null)
            {
                if (!sql.Discuss.Any(row => row.did == did)) return new
                      {
                          success = false,
                          msg = "帖子不存在"
                      };
                var parentTitle = sql.Discuss.First(row => row.did == did).title;
                return from d in sql.Discuss
                       from u in sql.User
                       from p in sql.Problem
                       where d.parentDid == did && u.username == d.username && p.pid == d.pid
                       select new discussTreeFormated(d.title, parentTitle, d.content)
                       {
                           id = d.did,
                           did = d.did,
                           parentDid = d.parentDid,
                           len = d.len,
                           nickname = u.nickname,
                           username = d.username,
                           time = d.time.toTimeString(),
                           pid = d.pid,
                           problemTitle = p.title,
                           leaf = !sql.Discuss.Any(row => row.parentDid == d.did)
                       };
            }
            else
            {
                if (!sql.Problem.Any(row => row.pid == pid)) return new
                {
                    success = false,
                    msg = "题目不存在"
                };
                return from d in sql.Discuss
                       from u in sql.User
                       from p in sql.Problem
                       where d.pid == pid && !d.parentDid.HasValue && u.username == d.username && p.pid == d.pid
                       select new
                       {
                           id = d.did,
                           d.did,
                           d.parentDid,
                           d.title,
                           d.len,
                           u.nickname,
                           u.username,
                           time = d.time.toTimeString(),
                           d.pid,
                           problemTitle = p.title,
                           leaf = !sql.Discuss.Any(row => row.parentDid == d.did)
                       };
            }
        }

        public class discussTreeFormated
        {
            public int id;
            public int did;
            public int? parentDid;
            public string title;
            public int len;
            public string nickname;
            public string username;
            public string time;
            public int pid;
            public string problemTitle;
            public bool leaf;

            public discussTreeFormated(string title, string parentTitle, string content)
            {
                if (title.EndsWith(parentTitle))
                {
                    content = functional.stripHtml(content).Replace("\r\n", "\n").Replace('\n', ' ').Trim();
                    if (content.Length > 50) content = content.Substring(0, 47) + "...";
                    this.title = content;
                }
                else this.title = title;
            }
        }
    }
}